package com.augurit.wf.bi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.wf.bi.convert.WfViewConfigConvertor;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfViewConfigService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfViewConfigForm;
import com.augurit.wf.common.dao.WfViewConfigDao;
import com.augurit.wf.common.entity.WfViewConfig;

@Service
@Transactional
public class WfViewConfigServiceImpl implements IWfViewConfigService{

	@Autowired
	private WfViewConfigDao wfViewConfigDao;
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	public WfViewConfigForm getByTemplateId(Long templateId){
		WfViewConfigForm form = null;
		if(templateId != null){
			String hql = "select view, temp.masterEntity from WfViewConfig view, WfBusTemplate temp where view.templateId = temp.id and view.templateId = ?";
			Object[] objs = wfViewConfigDao.findUnique(hql, templateId);
			
			if(objs != null){
				form = WfViewConfigConvertor.convertVoToForm((WfViewConfig)objs[0]);
				form.setMasterEntity((String)objs[1]);
			}
		}
		
		return form;
	}
	
	public WfViewConfigForm getByTemplateCode(String templateCode){
		if(templateCode == null){
			throw new IllegalArgumentException("templateCode is null!");
		}
		
		WfViewConfigForm form = null;
		String hql = "select view, temp.masterEntity from WfViewConfig view, WfBusTemplate temp where view.templateId = temp.id and temp.templateCode = ?";
		Object[] objs = wfViewConfigDao.findUnique(hql, templateCode);
		
		if(objs != null){
			form = WfViewConfigConvertor.convertVoToForm((WfViewConfig)objs[0]);
			form.setMasterEntity((String)objs[1]);
		}
		return form;
	}
	
	/**
	 * 获取关联主实体名
	 * @param templateId 模板id
	 * @return 关联主实体名
	 */
	public String getMasterEntityName(Long templateId){
		if(templateId != null){
			WfBusTemplateForm  busTemplateForm = wfBusTemplateService.get(templateId);
			String entityName = busTemplateForm.getMasterEntity();
			return entityName;
		}
		return null;
	}
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WfViewConfigForm get(Long id) {
		WfViewConfig entity = wfViewConfigDao.get(id);
		return WfViewConfigConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		wfViewConfigDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WfViewConfigForm... forms) {
		if(forms != null)
			for(WfViewConfigForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfViewConfigForm form){
		
		if(form != null){
			WfViewConfig entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = wfViewConfigDao.get(form.getId());
			}else{
				entity = new WfViewConfig();
			}
			
			//属性值转换
			WfViewConfigConvertor.convertFormToVo(form, entity);
			
			//保存
			wfViewConfigDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfViewConfigForm> search(Page<WfViewConfigForm> page, WfViewConfigForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from WfViewConfig ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		wfViewConfigDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfViewConfigForm> list = WfViewConfigConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfViewConfigForm> search(Page<WfViewConfigForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WfViewConfig> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		wfViewConfigDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfViewConfigForm> list = WfViewConfigConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
